home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / AppletView.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  8.8 KB  |  302 lines

  1. package horst;
  2.  
  3. import java.applet.Applet;
  4. import java.applet.AppletContext;
  5. import java.applet.AppletStub;
  6. import java.applet.AudioClip;
  7. import java.awt.Color;
  8. import java.awt.Component;
  9. import java.awt.FontMetrics;
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.Rectangle;
  13. import java.awt.Shape;
  14. import java.awt.Toolkit;
  15. import java.net.URL;
  16. import java.util.Enumeration;
  17. import java.util.Hashtable;
  18. import java.util.StringTokenizer;
  19. import java.util.Vector;
  20.  
  21. public class AppletView extends ComponentView implements AppletStub, AppletContext {
  22.    URL m_codeBaseURL;
  23.    int m_width;
  24.    int m_height;
  25.    float m_pctWidth;
  26.    float m_pctHeight;
  27.    Hashtable m_params;
  28.  
  29.    public AppletView(View parent, Element e, HTMLPane container) {
  30.       super(parent, e, container);
  31.    }
  32.  
  33.    public void appletResize(int width, int height) {
  34.       if (super.m_comp != null && (width >= 0 || height >= 0)) {
  35.          super.m_comp.setSize(width, height);
  36.       }
  37.  
  38.    }
  39.  
  40.    protected void flushResources() {
  41.       if (super.m_comp != null) {
  42.          ((Applet)super.m_comp).stop();
  43.          ((Applet)super.m_comp).destroy();
  44.       }
  45.  
  46.    }
  47.  
  48.    public final Applet getApplet(String name) {
  49.       return null;
  50.    }
  51.  
  52.    public final AppletContext getAppletContext() {
  53.       return this;
  54.    }
  55.  
  56.    public final Enumeration getApplets() {
  57.       Vector v = new Vector();
  58.       v.addElement(this);
  59.       return v.elements();
  60.    }
  61.  
  62.    public final AudioClip getAudioClip(URL url) {
  63.       return null;
  64.    }
  65.  
  66.    public final URL getCodeBase() {
  67.       return this.m_codeBaseURL != null ? this.m_codeBaseURL : this.getDocumentBase();
  68.    }
  69.  
  70.    public URL getDocumentBase() {
  71.       return super.m_elem.getDocument().getBaseURL();
  72.    }
  73.  
  74.    public final Image getImage(URL url) {
  75.       return Toolkit.getDefaultToolkit().getImage(url);
  76.    }
  77.  
  78.    protected int getMinimumSpan(int axis) {
  79.       return this.getPreferredSpan(axis);
  80.    }
  81.  
  82.    public final String getParameter(String name) {
  83.       return (String)this.m_params.get(name);
  84.    }
  85.  
  86.    protected int getPreferredSpan(int axis) {
  87.       switch (axis) {
  88.          case 0:
  89.             if (this.m_height != -1) {
  90.                return this.m_height;
  91.             }
  92.  
  93.             return 100;
  94.          case 1:
  95.             if (this.m_width != -1) {
  96.                return this.m_width;
  97.             }
  98.  
  99.             return 100;
  100.          default:
  101.             return 100;
  102.       }
  103.    }
  104.  
  105.    protected void init() {
  106.       this.m_width = 100;
  107.       this.m_pctWidth = -1.0F;
  108.       String value = (String)super.m_elem.getAttribute("width");
  109.       if (value != null) {
  110.          int idx = value.indexOf("%");
  111.          if (idx > 0) {
  112.             value = value.substring(0, idx);
  113.             Integer iVal = Utilities.getInteger(value);
  114.             if (iVal != null && iVal > 0 && iVal <= 100) {
  115.                this.m_pctWidth = (float)iVal / 100.0F;
  116.                this.m_width = -1;
  117.             }
  118.          } else {
  119.             Integer iVal = Utilities.getInteger(value);
  120.             this.m_width = iVal;
  121.          }
  122.       }
  123.  
  124.       this.m_height = 100;
  125.       this.m_pctHeight = -1.0F;
  126.       value = (String)super.m_elem.getAttribute("height");
  127.       if (value != null) {
  128.          int idx = value.indexOf("%");
  129.          if (idx > 0) {
  130.             value = value.substring(0, idx);
  131.             Integer iVal = Utilities.getInteger(value);
  132.             if (iVal != null && iVal > 0 && iVal <= 100) {
  133.                this.m_pctHeight = (float)iVal / 100.0F;
  134.                this.m_height = -1;
  135.             }
  136.          } else {
  137.             Integer iVal = Utilities.getInteger(value);
  138.             this.m_height = iVal;
  139.          }
  140.       }
  141.  
  142.       this.m_params = new Hashtable();
  143.  
  144.       for(int i = 0; i < super.m_elem.getElementCount(); ++i) {
  145.          Element child = super.m_elem.getElementAt(i);
  146.          if (child.getType() == 44) {
  147.             String name = (String)child.getAttribute("name");
  148.             value = (String)child.getAttribute("value");
  149.             if (name != null && value != null) {
  150.                this.m_params.put(name, value);
  151.             }
  152.          }
  153.       }
  154.  
  155.    }
  156.  
  157.    public boolean isActive() {
  158.       return true;
  159.    }
  160.  
  161.    protected Rectangle layout(int x, int y, int width, LayoutInfo info) {
  162.       int renderWidth;
  163.       if ((double)this.m_pctWidth != (double)-1.0F) {
  164.          renderWidth = (int)(this.m_pctWidth * (float)width);
  165.       } else {
  166.          renderWidth = this.getPreferredSpan(1);
  167.       }
  168.  
  169.       super.m_bounds.setBounds(x, y, renderWidth, this.getPreferredSpan(0));
  170.       if (super.m_comp != null) {
  171.          super.m_comp.setBounds(super.m_bounds);
  172.       }
  173.  
  174.       return super.m_bounds;
  175.    }
  176.  
  177.    protected void loadApplet(String base, String className) {
  178.       super.m_container.notifyStatusListeners(7, "Loading applet " + className + "...");
  179.       Class appletClass = null;
  180.  
  181.       try {
  182.          View.m_classLoader.setURLString(base);
  183.          appletClass = View.m_classLoader.loadClass(className);
  184.       } catch (Exception ex) {
  185.          ((Throwable)ex).printStackTrace();
  186.          return;
  187.       }
  188.  
  189.       try {
  190.          super.m_comp = (Component)appletClass.newInstance();
  191.          Applet applet = (Applet)super.m_comp;
  192.          applet.setStub(this);
  193.          int initWidth = this.m_width != -1 ? this.m_width : 100;
  194.          int initHeight = this.m_height != -1 ? this.m_height : 100;
  195.          ((Component)applet).setSize(initWidth, initHeight);
  196.          super.m_container.add(applet);
  197.          applet.init();
  198.          applet.start();
  199.       } catch (Exception ex) {
  200.          ((Throwable)ex).printStackTrace();
  201.       }
  202.    }
  203.  
  204.    protected void loadResources() {
  205.       if (super.m_container.m_preferences.m_bLoadApplets) {
  206.          String val = (String)super.m_elem.getAttribute("code");
  207.          if (val != null) {
  208.             String className = val;
  209.             int idx = val.indexOf(".class");
  210.             if (idx != -1) {
  211.                className = val.substring(0, idx);
  212.             }
  213.  
  214.             String base = className;
  215.             URL baseURL = super.m_container.getDocument().getBaseURL();
  216.             if (baseURL != null) {
  217.                base = baseURL.toString();
  218.                int lastBackSlash = base.lastIndexOf(47);
  219.                int lastDot = base.lastIndexOf(46);
  220.                if (lastDot > lastBackSlash) {
  221.                   base = base.substring(0, lastBackSlash);
  222.                }
  223.             }
  224.  
  225.             base = Utilities.removeTrailingBackslashes(base);
  226.             String codebase = (String)super.m_elem.getAttribute("codebase");
  227.             if (codebase != null) {
  228.                boolean bRelative = true;
  229.                if (!codebase.startsWith("/")) {
  230.                   URL testURL = Utilities.getURL(codebase);
  231.                   if (testURL != null) {
  232.                      bRelative = false;
  233.                      base = codebase;
  234.                   }
  235.                }
  236.  
  237.                if (bRelative) {
  238.                   if (!codebase.startsWith("/")) {
  239.                      codebase = "/" + codebase;
  240.                   }
  241.  
  242.                   base = base + codebase;
  243.                }
  244.  
  245.                base = Utilities.removeTrailingBackslashes(base);
  246.             }
  247.  
  248.             this.m_codeBaseURL = Utilities.getURL(base);
  249.             String archive = (String)super.m_elem.getAttribute("archive");
  250.             if (archive != null) {
  251.                StringTokenizer tok = new StringTokenizer(archive, ", ", false);
  252.  
  253.                while(tok.hasMoreTokens()) {
  254.                   String file = tok.nextToken();
  255.                   if (file.endsWith(".jar") || file.endsWith(".JAR") || file.endsWith(".zip") || file.endsWith(".ZIP")) {
  256.                      try {
  257.                         View.m_classLoader.loadJAR(base, file);
  258.                      } catch (Exception e) {
  259.                         ((Throwable)e).printStackTrace();
  260.                         return;
  261.                      }
  262.                   }
  263.                }
  264.             }
  265.  
  266.             this.loadApplet(base, className);
  267.          }
  268.  
  269.       }
  270.    }
  271.  
  272.    public void paint(Graphics g, Shape alloc) {
  273.       if (super.m_comp == null) {
  274.          Color oldColor = g.getColor();
  275.          g.setColor(Color.blue);
  276.          g.drawRect(super.m_bounds.x, super.m_bounds.y, super.m_bounds.width, super.m_bounds.height);
  277.          String msg = "Applet missing!";
  278.          FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(((View)this).getFont());
  279.          int strWidth = fm.stringWidth(msg);
  280.          if (strWidth <= super.m_bounds.width && fm.getHeight() <= super.m_bounds.height) {
  281.             int offset = (super.m_bounds.width - strWidth) / 2;
  282.             g.drawString(msg, super.m_bounds.x + offset, super.m_bounds.y + fm.getHeight());
  283.          }
  284.  
  285.          g.setColor(oldColor);
  286.       }
  287.  
  288.       ((View)this).drawDebugBox(g, Color.blue);
  289.    }
  290.  
  291.    public void showDocument(URL url) {
  292.       super.m_container.openPage(url);
  293.    }
  294.  
  295.    public void showDocument(URL url, String target) {
  296.    }
  297.  
  298.    public void showStatus(String text) {
  299.       super.m_container.notifyStatusListeners(7, text);
  300.    }
  301. }
  302.